package com.jiyi.activity.datainput.sm;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiyi.activity.datainput.DIBaseMorePicActivity;
import com.jiyi.bean.SingleImageBean;
import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.SafeEvacuationBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.inteface.DownloadPicOverInterface;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.NoScrollGridView;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.base.AppMediaType;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MultipartBody;

/**
 * Created by Administrator on 2018/3/22.
 */

public class SafeEvacuationActivity extends DIBaseMorePicActivity implements DownloadPicOverInterface{
    /* 头部 */
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.rbSettingYes)
    RadioButton rbSettingYes;
    @BindView(R.id.rbSettingNo)
    RadioButton rbSettingNo;
    @BindView(R.id.rgSettingIsOK)
    RadioGroup rgSettingIsOK;
    @BindView(R.id.etSettingProblem)
    EditText etSettingProblem;
    @BindView(R.id.etSettingMeasure)
    EditText etSettingMeasure;
    @BindView(R.id.rbExitYes)
    RadioButton rbExitYes;
    @BindView(R.id.rbExitNo)
    RadioButton rbExitNo;
    @BindView(R.id.rgExitHasUse)
    RadioGroup rgExitHasUse;
    @BindView(R.id.etExitProblem)
    EditText etExitProblem;
    @BindView(R.id.etExitMeasure)
    EditText etExitMeasure;
    @BindView(R.id.rbMaterialYes)
    RadioButton rbMaterialYes;
    @BindView(R.id.rbMaterialNo)
    RadioButton rbMaterialNo;
    @BindView(R.id.rgMaterialIsOk)
    RadioGroup rgMaterialIsOk;
    @BindView(R.id.etMaterialProblem)
    EditText etMaterialProblem;
    @BindView(R.id.etMaterialMeasure)
    EditText etMaterialMeasure;
    @BindView(R.id.rbRefugeFloorsYes)
    RadioButton rbRefugeFloorsYes;
    @BindView(R.id.rbRefugeFloorsNo)
    RadioButton rbRefugeFloorsNo;
    @BindView(R.id.rgHasRefugeFloorsUse)
    RadioGroup rgHasRefugeFloorsUse;
    @BindView(R.id.etRefugeFloorsProblem)
    EditText etRefugeFloorsProblem;
    @BindView(R.id.etRefugeFloorsMeasure)
    EditText etRefugeFloorsMeasure;
    @BindView(R.id.rbRefugeFloorsAcrossYes)
    RadioButton rbRefugeFloorsAcrossYes;
    @BindView(R.id.rbRefugeFloorsAcrossNo)
    RadioButton rbRefugeFloorsAcrossNo;
    @BindView(R.id.rgHasRefugeFloorsAcross)
    RadioGroup rgHasRefugeFloorsAcross;
    @BindView(R.id.etRefugeFloorsAcrossProblem)
    EditText etRefugeFloorsAcrossProblem;
    @BindView(R.id.etRefugeFloorsAcrossMeasure)
    EditText etRefugeFloorsAcrossMeasure;

    int mSettingIsOK, mExitHasUse, mMaterialIsOk, mHasRefugeFloorsUse, mHasRefugeFloorsAcross;
    String build_id;

    @BindView(R.id.gvSetting)
    NoScrollGridView gvSetting;
    @BindView(R.id.gvExit)
    NoScrollGridView gvExit;
    @BindView(R.id.gvMaterial)
    NoScrollGridView gvMaterial;
    @BindView(R.id.gvRefugeFloors)
    NoScrollGridView gvRefugeFloors;
    @BindView(R.id.gvRefugeFloorsAcross)
    NoScrollGridView gvRefugeFloorsAcross;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safe_evacuation);
        ButterKnife.bind(this);
        init();
    }

    public void init() {
        getIntentData();
        setTop();
        setViewStatus();
        getData();
        super.setDownloadPicOverInterface(this);
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("安全疏散与避难");
        tvSearch.setCompoundDrawables(null, null, null, null);
        tvSearch.setText("保存");
    }

    public void setViewStatus() {
        rgSettingIsOK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbSettingYes) {
                    mSettingIsOK = 1;
                } else {
                    mSettingIsOK = 0;
                }
            }
        });

        rgExitHasUse.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbExitYes) {
                    mExitHasUse = 1;
                } else {
                    mExitHasUse = 0;
                }
            }
        });

        rgMaterialIsOk.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbMaterialYes) {
                    mMaterialIsOk = 1;
                } else {
                    mMaterialIsOk = 0;
                }
            }
        });

        rgHasRefugeFloorsUse.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbRefugeFloorsYes) {
                    mHasRefugeFloorsUse = 1;
                } else if (checkedId == R.id.rbRefugeFloorsNo) {
                    mHasRefugeFloorsUse = 0;
                } else {
                    mHasRefugeFloorsUse = 2;
                }
            }
        });

        rgHasRefugeFloorsAcross.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbRefugeFloorsAcrossYes) {
                    mHasRefugeFloorsAcross = 1;
                } else if (checkedId == R.id.rbRefugeFloorsAcrossNo) {
                    mHasRefugeFloorsAcross = 0;
                } else {
                    mHasRefugeFloorsAcross = 2;
                }
            }
        });
    }


    public void getData() {
        HttpMethods.getInstance().getSafeEvacuationInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<SafeEvacuationBean>(this) {
                    @Override
                    public void onNext(SafeEvacuationBean safeEvacuationBean) {
                        if (safeEvacuationBean == null || "".equals(safeEvacuationBean.getGson_null())) {
                            safeEvacuationBean = new SafeEvacuationBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(0);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            safeEvacuationBean.setSetting_meet_standard(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(0);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            safeEvacuationBean.setIs_occupy(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(0);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            safeEvacuationBean.setDecoration_meet_standard(bc3);
                            BaseCheck bc4 = new BaseCheck();
                            bc4.setIs_ok(0);
                            bc4.setProblem("");
                            bc4.setMeasures("");
                            safeEvacuationBean.setRefuge_is_occupied(bc4);
                            BaseCheck bc5 = new BaseCheck();
                            bc5.setIs_ok(0);
                            bc5.setProblem("");
                            bc5.setMeasures("");
                            safeEvacuationBean.setRefuge_is_crossing(bc5);
                            safeEvacuationBean.setNo_edit(1);
                        }

                        fillData(safeEvacuationBean);
                    }
                });
    }

    private void fillData(SafeEvacuationBean safeEvacuationBean) {
        if (safeEvacuationBean.getSetting_meet_standard().getIs_ok() == 1) {
            rbSettingYes.setChecked(true);
        }

        etSettingProblem.setText(safeEvacuationBean.getSetting_meet_standard().getProblem());
        etSettingMeasure.setText(safeEvacuationBean.getSetting_meet_standard().getMeasures());

        if (safeEvacuationBean.getIs_occupy().getIs_ok() == 1) {
            rbExitYes.setChecked(true);
        }

        etExitProblem.setText(safeEvacuationBean.getIs_occupy().getProblem());
        etExitMeasure.setText(safeEvacuationBean.getIs_occupy().getMeasures());

        if (safeEvacuationBean.getDecoration_meet_standard().getIs_ok() == 1) {
            rbMaterialYes.setChecked(true);
        }

        etMaterialProblem.setText(safeEvacuationBean.getDecoration_meet_standard().getProblem());
        etMaterialMeasure.setText(safeEvacuationBean.getDecoration_meet_standard().getMeasures());

        if (safeEvacuationBean.getRefuge_is_occupied().getIs_ok() == 1) {
            rbRefugeFloorsYes.setChecked(true);
        } else if (safeEvacuationBean.getRefuge_is_occupied().getIs_ok() == 0) {
            rbRefugeFloorsNo.setChecked(true);
        }

        etRefugeFloorsProblem.setText(safeEvacuationBean.getRefuge_is_occupied().getProblem());
        etRefugeFloorsMeasure.setText(safeEvacuationBean.getRefuge_is_occupied().getMeasures());

        if (safeEvacuationBean.getRefuge_is_crossing().getIs_ok() == 1) {
            rbRefugeFloorsAcrossYes.setChecked(true);
        } else if (safeEvacuationBean.getRefuge_is_crossing().getIs_ok() == 0) {
            rbRefugeFloorsAcrossNo.setChecked(true);
        }

        etRefugeFloorsAcrossProblem.setText(safeEvacuationBean.getRefuge_is_crossing().getProblem());
        etRefugeFloorsAcrossMeasure.setText(safeEvacuationBean.getRefuge_is_crossing().getMeasures());

        if (safeEvacuationBean.getNo_edit() == 1) {
            tvSearch.setVisibility(View.VISIBLE);
        } else {
            tvSearch.setVisibility(View.INVISIBLE);
        }

        initGridViewList(safeEvacuationBean);
    }

    private void doPost() {
        Map<String, String> a1 = new HashMap<>();
        a1.put("is_ok", mSettingIsOK + "");
        a1.put("problem", etSettingProblem.getText().toString());
        a1.put("measures", etSettingMeasure.getText().toString());

        Map<String, String> a2 = new HashMap<>();
        a2.put("is_ok", mExitHasUse + "");
        a2.put("problem", etExitProblem.getText().toString());
        a2.put("measures", etExitMeasure.getText().toString());

        Map<String, String> a3 = new HashMap<>();
        a3.put("is_ok", mMaterialIsOk + "");
        a3.put("problem", etMaterialProblem.getText().toString());
        a3.put("measures", etMaterialMeasure.getText().toString());

        Map<String, String> a4 = new HashMap<>();
        a4.put("is_ok", mHasRefugeFloorsUse + "");
        a4.put("problem", etRefugeFloorsProblem.getText().toString());
        a4.put("measures", etRefugeFloorsMeasure.getText().toString());

        Map<String, String> a5 = new HashMap<>();
        a5.put("is_ok", mHasRefugeFloorsAcross + "");
        a5.put("problem", etRefugeFloorsAcrossProblem.getText().toString());
        a5.put("measures", etRefugeFloorsAcrossMeasure.getText().toString());

        Map<String, Map<String, String>> dataParams = new HashMap<>();
        dataParams.put("setting_meet_standard", a1);
        dataParams.put("is_occupy", a2);
        dataParams.put("decoration_meet_standard", a3);
        dataParams.put("refuge_is_occupied", a4);
        dataParams.put("refuge_is_crossing", a5);

        MultipartBody.Builder builder = new MultipartBody.Builder();

        putBody(builder, AppConfig.ACCESS_TOKEN, AppMediaType.TEXT, UserInfoUtil.getCurrentUserAccessToken());
        putBody(builder, "build_id", AppMediaType.TEXT, build_id);
        putBody(builder, "type", AppMediaType.TEXT, 1 + "");
        putBody(builder, "data", AppMediaType.TEXT, new Gson().toJson(dataParams));
        putBody(builder, "action_type", AppMediaType.TEXT, "safe_evacuat");


        // 图片数组
        File file;
        int size = mulGridViewBean.getSingleImageList().size();
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < mulGridViewBean.getSingleImageList().get(i).getImageList().size(); j++) {
                if (mulGridViewBean.getSingleImageList().get(i).getImageList().get(j).contains("http")) {
                    Log.e("ttt     ", mulGridViewBean.getSingleImageList().get(i).getImageList().get(j) + "\n");
                    continue;
                } else {
                    file = new File(mulGridViewBean.getSingleImageList().get(i).getImageList().get(j));
                    if (i == 0) {
                        putBody(builder, "image*setting_meet_standard[]", AppMediaType.JPG, file);
                    } else if (i == 1) {
                        putBody(builder, "image*is_occupy[]", AppMediaType.JPG, file);
                    } else if (i == 2) {
                        putBody(builder, "image*decoration_meet_standard[]", AppMediaType.JPG, file);
                    }
                    else if (i == 3) {
                        putBody(builder, "image*refuge_is_occupied[]", AppMediaType.JPG, file);
                    }
                    else if (i == 4) {
                        putBody(builder, "image*refuge_is_crossing[]", AppMediaType.JPG, file);
                    }
                    else {
                        continue;
                    }
                }
            }
        }

        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();

        HttpMethods.getInstance().postSafeEvacuationInfo(multipartBody)
                .subscribe(new SampleProgressObserver<SafeEvacuationBean>(this) {
                    @Override
                    public void onNext(SafeEvacuationBean safeEvacuationBean) {
                        ToastUtil.getInstances().showMsg("保存成功");
                    }
                });
    }

    void initGridViewList(SafeEvacuationBean safeEvacuationBean) {
        List<SingleImageBean> singleImageBeans = new ArrayList<SingleImageBean>();

        SingleImageBean s1 = new SingleImageBean();
        s1.setPosition(0);
        s1.setDelModel(false);
        s1.setGridView(gvSetting);
        s1.setImageList(safeEvacuationBean.getSetting_meet_standard().getImage());
        singleImageBeans.add(s1);

        SingleImageBean s2 = new SingleImageBean();
        s2.setPosition(1);
        s2.setDelModel(false);
        s2.setGridView(gvExit);
        s2.setImageList(safeEvacuationBean.getIs_occupy().getImage());
        singleImageBeans.add(s2);

        SingleImageBean s3 = new SingleImageBean();
        s3.setPosition(2);
        s3.setDelModel(false);
        s3.setGridView(gvMaterial);
        s3.setImageList(safeEvacuationBean.getDecoration_meet_standard().getImage());
        singleImageBeans.add(s3);

        SingleImageBean s4 = new SingleImageBean();
        s4.setPosition(3);
        s4.setDelModel(false);
        s4.setGridView(gvRefugeFloors);
        s4.setImageList(safeEvacuationBean.getRefuge_is_occupied().getImage());
        singleImageBeans.add(s4);

        SingleImageBean s5 = new SingleImageBean();
        s5.setPosition(4);
        s5.setDelModel(false);
        s5.setGridView(gvRefugeFloorsAcross);
        s5.setImageList(safeEvacuationBean.getRefuge_is_crossing().getImage());
        singleImageBeans.add(s5);

        mulGridViewBean.setSingleImageList(singleImageBeans);
        refreshAdapter();
    }

    @OnClick({R.id.tvBack, R.id.tvSearch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            case R.id.tvSearch:
                downloadImage();
                break;
            default:
                break;
        }
    }

    @Override
    public void post() {
        doPost();
    }
}
